package com.qinyh.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.Toast;

import com.qinyh.activity.MenuActivity;
import com.qinyh.activity.R;
import com.qinyh.entity.Book;
import com.qinyh.entity.BookCatalog;
import com.qinyh.entity.Point;
import com.qinyh.util.DebugUtil;
import com.qinyh.util.TemplateStore;

public class CatalogView extends ImageView {

	public CatalogView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public CatalogView(Context context) {
		super(context);
		this.ctt = context;
	}

	private BookCatalog catalog;
	private Context ctt;
	private AttributeSet attrs;
	private Point start;
	private Point end;
	private long downTime;
	private long eventTime;
	private Activity activity;
	private boolean isStart = false;
	private Bitmap bookCover = null;

	public Context getCtt() {
		return ctt;
	}

	public void setCtt(Context ctt) {
		this.ctt = ctt;
	}

	public void setActivity(Activity activity) {
		this.activity = activity;
	}

	public AttributeSet getAttrs() {
		return attrs;
	}

	public void setAttrs(AttributeSet attrs) {
		this.attrs = attrs;
	}

	public BookCatalog getCatalog() {
		return catalog;
	}

	public void setCatalog(BookCatalog catalog) {
		this.catalog = catalog;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// super.onDraw(canvas);
		try {
			Paint paint = new Paint();
			paint.setTextSize(TemplateStore.fontSize);
			paint.setColor(Color.WHITE);
			canvas.drawText(catalog.getCatalogName(), 20, 30, paint);
			int count = 0;
			for (Book b : catalog.getBooks()) {
				Bitmap bitmap = BitmapFactory.decodeByteArray(b.getCovers(), 0,
						b.getCovers().length);
				Matrix matrix = new Matrix();
				// matrix.postScale((float)activity.getWindowManager().getDefaultDisplay().getWidth()/3/bitmap.getWidth(),
				// (float)activity.getWindowManager().getDefaultDisplay().getHeight()/5/bitmap.getHeight());
				float k = (float) (this.getHeight() - 60) / bitmap.getHeight();
				matrix.postScale(k, k);
				// Bitmap scaleBitmap = Bitmap.createBitmap(bitmap, 0, 0,
				// activity.getWindowManager().getDefaultDisplay().getWidth()/3,
				// activity.getWindowManager().getDefaultDisplay().getHeight()/5,
				// matrix, true);
				Bitmap scaleBitmap = Bitmap.createBitmap(bitmap, 0, 0,
						bitmap.getWidth(), bitmap.getHeight(), matrix, true);
				canvas.drawBitmap(scaleBitmap, 30 + count * 140, 50, paint);
				count++;
				width = bitmap.getWidth();
				height = bitmap.getHeight();
			}
		} catch (Exception exp) {
			DebugUtil.debug("debug", exp);
		}
	}

	private float width = 98;
	private float height = 132;

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (!isStart)
			switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN:
				start = new Point();
				start.setX(event.getX());
				start.setY(event.getY());
				break;
			case MotionEvent.ACTION_MOVE:
				break;
			case MotionEvent.ACTION_UP:
				end = new Point();
				end.setX(event.getX());
				end.setY(event.getY());
				downTime = event.getDownTime();
				eventTime = event.getEventTime();
				isClick();
				isStart = false;
				break;
			default:
				break;
			}
		if (!isStart)
			return true;
		else
			return false;
	}

	private void isClick() {
		if (eventTime - downTime < 350) {
			try {
				if (!isStart) {
					isStart = true;
					int a = clickSeq();
					if (a > 0) {
						// TODO 加动画
						LinearLayout linearLayout = new LinearLayout(ctt);
						Intent intent = new Intent(activity, MenuActivity.class);
						Book book = catalog.getBooks().get(a - 1);
						// 电子书封面
						bookCover = getBitmapFromByte(book.getCovers());
						BitmapDrawable drawable = new BitmapDrawable(bookCover);
						ImageView imageView = new ImageView(ctt);
						imageView.setImageDrawable(drawable);
						imageView.setAnimation(AnimationUtils.loadAnimation(
								ctt, R.anim.scale));
						linearLayout.setLayoutParams(new LayoutParams(	
								LayoutParams.FILL_PARENT,
								LayoutParams.FILL_PARENT));
						
						activity.setContentView(imageView);
//						activity.startActivity(intent);
						TemplateStore.setCurrentBook(book);
						// 动画伸缩
						activity.overridePendingTransition(R.anim.scale,
								R.anim.scale);
						isStart = false;
					}
				}
			} catch (Exception exp) {
				Toast.makeText(ctt, exp.toString(), Toast.LENGTH_LONG).show();
			}
		}
	}

	private int clickSeq() {
		if (start.getX() > 30 && start.getX() < 128 && start.getY() > 50
				&& start.getY() < 182)
			return 1;
		else if (start.getX() > 170 && start.getX() < 268 && start.getY() > 50
				&& start.getY() < 182)
			return 2;
		return 0;
	}

	/**
	 * 获得Bitmap位图
	 * 
	 * @param temp
	 * @return
	 */
	public Bitmap getBitmapFromByte(byte[] temp) {
		if (temp != null) {
			Bitmap bitmap = BitmapFactory.decodeByteArray(temp, 0, temp.length);
			return bitmap;
		} else {
			return null;
		}
	}

}
